<?php

namespace App\Http\Controllers\Api\Dream\Content\Community;

use App\Api\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\User;
use Doctrine\DBAL\Schema\AbstractAsset;
use Illuminate\Http\Request;
use App\Models\Dream\Content\Community\Community;
use App\Models\Dream\Content\Community\Image;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\FilterController as Filter;//敏感词过滤


class CommunityController extends Controller
{
    use ApiResponse;

    //获取研博圈信息 ，前端展示
    public function create()
    {
        $community_data = Community::where('content_status',2)->orderBy('updated_at','desc')->get();//研博圈标题
        $user = User::select('id','name','avatar')->get();

       $count =  $community_data->count();
        if(empty($count)){
            return $this->setStatusCode('201')->success('暂无评论数据','error');
        }

        foreach ($community_data as  $key=>$val){

            if(empty($val->images_url)){
                //图片为空时
                $msg[$key] = $val;

                //用户信息
                foreach ($user as $u_key=>$k_val){
                    if($val->user_id == $k_val->id ){
                        $msg[$key]['name'] = $k_val->name;
                        $msg[$key]['avatar'] = $k_val->avatar;
                    }
                }

            }else{
                $val->images_url = explode(',',$val->images_url);
                $msg[$key] = $val;
                //用户信息
                foreach ($user as $u_key=>$k_val){
                    if($val->user_id == $k_val->id ){
                        $msg[$key]['name'] = $k_val->name;
                        $msg[$key]['avatar'] = $k_val->avatar;
                    }
                }
            }
        }

        return $this->success($msg);
    }

    //新增：用户添加研博圈信息
    public function store(Request $request)
    {
        $user_data = Auth::guard('api')->user();
        $data['user_id'] = $user_data->id;
        $data['content'] = $request->contents;
        $image = $request->images;

        //App\Models\User词过滤:判断是否为敏感词，是则返回true，反之则为false
        $word_filter = Filter::isLegal($request->contents);
        if($word_filter){

            $filter['status'] = '评论含有敏感词';
            $filter['msg'] =  Filter::replace($request->contents,'*',true);

            return $this->setStatusCode('201')->success($filter,'error');
        }

        //必须条件，1,用户 2,评论权限 content_status 默认为 0
        if (empty( $data['user_id'])&& empty($user_data->content_status)){
            return $this->failed('没有此用户','201','success');
        }

        /**
         * 没有图片
         */
        if(empty($image)){
            //只提交“$data”标题信息
            $inset = Community::create($data);
            return  $this->success($inset);
        }

        /**
         * 有图片
         */
        $data_images = array_values($image);
        if(!is_array($data_images)){
            return $this->failed('数据非法，不是数组数据','201','success');
        }

        $data['images_url'] = implode(',',$data_images);
        $inset = Community::create($data);
        return  $this->success($inset);
    }

    //图片多张上传
    public function contentImage(Request $request)
    {
        $file = $_FILES['image'];
        //$community_id = $request->community_id; //图片对应的研博圈标题
        $user_id = $request->user_id;

        //简单判断用户信息
        $user = User::where('id',$user_id)->first();
        if(empty($user)){
            return  $this->failed('没有此用户','201','success');
        }

        //规定图片格式，防止违反规则的信息
        $type_name = substr($file['name'],strrpos($file['name'],'.')+1);//获取图片后缀
        $type_name_array = ['jpeg', 'jpg', 'gif', 'gpeg', 'png'];

        if(!in_array($type_name,$type_name_array)){
            return  $this->failed('没有此格式图片','201','success');
        }

        //图片保存的"目录"
        $date = date('Y-m-d');
        $uploadDir = 'app'.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'content'.DIRECTORY_SEPARATOR.$date.DIRECTORY_SEPARATOR.$user_id.DIRECTORY_SEPARATOR;

        //图片保存"文件"的相对路径,包含具体图片
        $uploadFile = $uploadDir.bin2hex(random_bytes(3)).basename($file['name']);

        /**
         * 创建图片保存的目录文件
         * storage_path(storage_path($uploadFile)) 对应storage目录下的绝对路径
         */
        if(!file_exists((storage_path($uploadDir)) )){
            mkdir(storage_path($uploadDir),0777,true);
        }

        //移动图片临时文件到具体目录
        if (move_uploaded_file($file['tmp_name'],storage_path($uploadFile))){

            $path = substr($uploadFile,11);
            $path_image = str_replace("\\",'/',$path); //将路径“\”全部转换为“/”

            $image_uel = config('app.url') . "/storage/".$path_image;

            return  $this->success($image_uel);
        }



    }


}
